package com.tzbank.poc.util;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.logging.SimpleFormatter;

/**
 * Create by li_zheng2 on 2019/1/21.
 */
public class IdUtil {

        public static volatile  int Guid = 100;
        public static int MAX = 999;
        /**
        * @Description:  获取20位不重复的随机会员ID
        * @param
        * @return: java.lang.String
        * @throws:
        * @Author: li_zheng2
        * @Date: 2019/1/21
        */
    public static String generatorId(){
        Guid +=1;
        long now = System.currentTimeMillis();
        //获取4位年份
        SimpleDateFormat formatter =  new SimpleDateFormat("yyyy");
        String time = formatter.format(now);
        //获取13位的时间戳
        String info = now +"";
        //生成3位的随机数
        int ran =0;
        if(Guid > MAX){
            Guid = 100;
        }
        ran = Guid;
        return time + info.substring(2,info.length()) + ran;
    }

    /**
     * 客户开户生成账号
     * 801101 机构号 + 账户类型12 1-个人 2-II类账户 + 用户号后8位
     */
    public static String getAccountId(String userNo){
        String accountId = String.join("801101","12");
        accountId = String.join(accountId,userNo.substring(8,17));
        return accountId;
    }

    /**
     * 生成交易流水
     *
     */
    public static String getTradeSeq(String userNo){
        Random random= new Random(Long.parseLong(userNo.substring(userNo.length()-4,userNo.length()))+System.currentTimeMillis());
        String rand= (Math.abs(random.nextInt(900))+100)+"";
        String orderid=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+rand;
        return orderid;
    }
}
